<?php
class AdminAwards extends CActiveRecord{

    public static function model($className = __CLASS__) {
        return parent::model ( $className );
    }

    // dat luat cho model
    public function rules(){}         

    // goi den bang can ket noi   
    public function tableName() {                  
        return 'awards';
    }
    
    public function countAllRow(){
        $sql = "SELECT count(id) as count FROM awards";
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow();
        return $row; 
    }
    
    public function getAllRowPaging($begin, $end){
        $row = array();
        $sql = "SELECT * FROM awards ORDER BY id DESC LIMIT ".$begin.",".$end;
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryAll();
        return $row;             
    }
    
    public function insertRow($name, $value,$description, $type, $image, $status, $order){
        $last_id =0;
        //echo $name."--".$value."--".$description."--". $type."--". $image."--". $status."--". $order;die;
        $connect = Yii::app()->db;
        $sql =   "INSERT INTO awards (name, value, description, type, image, ordering, status) VALUES (:name, :value, :description, :type, :image, :ordering, :status)";
        $command = $connect->createCommand ($sql);
        $command->bindValue ( ":name", $name, PDO::PARAM_STR );
        $command->bindValue ( ":value", $value, PDO::PARAM_STR );
        $command->bindValue ( ":description", $description, PDO::PARAM_STR );
        $command->bindValue ( ":type", $type, PDO::PARAM_STR );
        $command->bindValue ( ":image", $image, PDO::PARAM_STR );
        $command->bindValue ( ":ordering", $order, PDO::PARAM_STR ); 
        $command->bindValue ( ":status", $status, PDO::PARAM_STR );
          
        $result = $command->execute();
        $last_id = Yii::app()->db->getLastInsertId();
        return $last_id;
    }
    
    public function updateRowById($id, $name, $value, $description, $type, $image, $status, $order){
        $str = "";
        if($name != ""){
            $str .= "name ='".$name."',";
        }
        if($value != ""){
            $str .= "value ='".$value."',";
        }
        if($description != ""){
            $str .= "description ='".$description."',";
        }
        if($type != ""){
            $str .= "type = ".$type.",";
        }
        if($image != ""){
            $str .= "image ='".$image."',";
        }
        if(is_numeric($status)){
            $str .= "status =".$status.",";
        }
        if($order != ""){
            $str .= "`ordering` =".$order.",";
        }
        
        $r_str = rtrim($str,",");
        $sql = "UPDATE awards SET ".$r_str." WHERE id =".$id;

        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $result = $command->execute();
        return $result;     
    }
    
    public function deleteRowById($id){
        $sql = "DELETE FROM awards WHERE id=".$id;
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $result = $command->execute(); 
        return $result;          
    }
    
    public function getRowById($id){
        $row = array();
        $sql = "SELECT * FROM awards WHERE id=".$id;
        $connect = Yii::app()->db;
        $command = $connect->createCommand($sql);
        $row = $command->queryRow(); 
        return $row;          
    } 
}